<template>
	<view>
		<image mode="widthFix" :src="path" />
		<ksp-cropper mode="fixed" :width="600" :height="600" :maxWidth="1024" :maxHeight="1024" :url="iconUrl"
			@cancel="oncancel" @ok="onok"></ksp-cropper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				iconUrl: "",
				path: ""
			}
		},
		onLoad(options) {
			this.iconUrl = decodeURIComponent(options.path)
		},
		methods: {
			onok(ev) {
				this.iconUrl = "";
				this.path = ev.path;
				uni.uploadFile({
					url: this.$baseUrl + '/Base_Manage/Upload/UploadFileUserByForm',
					filePath: this.path,
					header: {
						'Authorization': uni.getStorageSync('tokenKey')
					},
					name: 'file',
				}).then(result => {
					let [err, res] = [...result]
					if (err !== null) return uni.showToast({
						icon: 'none',
						title: '网络连接失败,请重试'
					})
					this.save(JSON.parse(res.data).thumbUrl)
				})
			},
			oncancel() {
				// url设置为空，隐藏控件
				this.iconUrl = "";
				uni.navigateBack()
			},
			save(url) {
				let userInfo = uni.getStorageSync('userInfo')
				userInfo.UserAvatarURL = url
				this.$http('post', '/Base_Manage/Base_User/SaveData', userInfo).then(res => {
					if (res.Success) {
						uni.setStorageSync('userInfo', userInfo)
						uni.navigateBack()
					}
				})
			}
		}
	}
</script>